package com.example.demo.controller;

import com.example.demo.model.TextDocument;
import com.example.demo.model.User;
import com.example.demo.service.TextDocumentService;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("/dashboard/documents")
public class DocumentController {

    private final TextDocumentService textDocumentService;
    private final UserService userService;

    public DocumentController(TextDocumentService textDocumentService, UserService userService) {
        this.textDocumentService = textDocumentService;
        this.userService = userService;
    }

    @GetMapping
    public String listDocuments(Model model) {
        model.addAttribute("documents", textDocumentService.getAllDocuments());
        model.addAttribute("user", userService.getCurrentUser());
        return "dashboard/documents/list";
    }

    @GetMapping("/create")
    public String createDocumentForm(Model model) {
        model.addAttribute("document", new TextDocument());
        model.addAttribute("user", userService.getCurrentUser());
        return "dashboard/documents/form";
    }

    @PostMapping("/create")
    public String createDocument(@ModelAttribute TextDocument document, RedirectAttributes redirectAttributes) {
        try {
            User currentUser = userService.getCurrentUser();
            document.setAuthor(currentUser.getUsername());
            textDocumentService.saveDocument(document);
            redirectAttributes.addFlashAttribute("successMessage", "文档创建成功");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("errorMessage", e.getMessage());
        }
        return "redirect:/dashboard/documents";
    }

    @GetMapping("/edit/{id}")
    public String editDocumentForm(@PathVariable Long id, Model model) {
        model.addAttribute("document", textDocumentService.getDocumentById(id));
        model.addAttribute("user", userService.getCurrentUser());
        return "dashboard/documents/form";
    }

    @PostMapping("/edit/{id}")
    public String updateDocument(@PathVariable Long id, @ModelAttribute TextDocument document, RedirectAttributes redirectAttributes) {
        try {
            TextDocument existingDocument = textDocumentService.getDocumentById(id);
            existingDocument.setTitle(document.getTitle());
            existingDocument.setContent(document.getContent());
            existingDocument.setFeatured(document.isFeatured());
            textDocumentService.saveDocument(existingDocument);
            redirectAttributes.addFlashAttribute("successMessage", "文档更新成功");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("errorMessage", e.getMessage());
        }
        return "redirect:/dashboard/documents";
    }

    @GetMapping("/delete/{id}")
    public String deleteDocument(@PathVariable Long id, RedirectAttributes redirectAttributes) {
        try {
            textDocumentService.deleteDocument(id);
            redirectAttributes.addFlashAttribute("successMessage", "文档已删除");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("errorMessage", e.getMessage());
        }
        return "redirect:/dashboard/documents";
    }

    @GetMapping("/toggle-featured/{id}")
    public String toggleFeatured(@PathVariable Long id, RedirectAttributes redirectAttributes) {
        try {
            textDocumentService.toggleFeatured(id);
            redirectAttributes.addFlashAttribute("successMessage", "文档首页显示状态已更新");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("errorMessage", e.getMessage());
        }
        return "redirect:/dashboard/documents";
    }
} 